from fastapi import APIRouter, Query
from tortoise.expressions import Q

from app.controllers.goods.category import category_controller
from app.controllers.goods.goods import goods_controller
from app.controllers.goods.sku import sku_controller
from app.schemas.base import Success, SuccessExtra, Fail
from app.models.mini import MiniFav
from typing import Union
from app.core.ctx import CTX_USER_ID
from app.core.dependency import DependCustomerAuth

mini_all_goods_router = APIRouter()
model_name = "商品"


@mini_all_goods_router.get("/list", summary=f"查看{model_name}列表", dependencies=[DependCustomerAuth])
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        category_id: int = Query(None, description=f"分类id"),
        space_id: int = Query(0, description=f"门店id"),
        name: str = Query(None, description=f"商品名称"),
        type: int = Query(0, description=f"商品类型1点单2商城3储值"),
):
    user_id = CTX_USER_ID.get()
    q = Q(state=True)
    if category_id:
        cate_ids = await category_controller.get_tree_ids(category_id)
        q &= Q(category_id__in=cate_ids)

    if name:
        q &= Q(name__icontains=name)
    if type > 0:
        q &= Q(type=type)
    if space_id > 0:
        q &= Q(space_id__contains=[space_id])
    total, data = await goods_controller.do_list(page=page, page_size=page_size, search=q)

    category_objs = await goods_controller.categorys()
    for item in data:
        if item["category_id"]:
            item["lm"] = category_objs[item["category_id"]]
        if item["sku"]:
            sku = item["sku"]
            kc, min_p, max_p = await goods_controller.sku_format(sku=sku)
            item["price"] = f"￥{min_p}~{max_p}"
            item["kc"] = kc
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)


@mini_all_goods_router.get("/get", summary=f"查看{model_name}", dependencies=[DependCustomerAuth])
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    user_id = CTX_USER_ID.get()
    like = False
    data = await goods_controller.do_detail(data_id=data_id)
    if user_id:
        like_data = await MiniFav.filter(user_id=user_id).filter(type_id=data['id']).filter(type=1).first()
        if like_data:
            like = True
    if data["sku"]:
        sku = data["sku"]
        kc, min_p, max_p = await goods_controller.sku_format(sku=sku)
        data["price"] = f"￥{min_p}~{max_p}"
        data["kc"] = kc
        data["like"] = like
    return Success(data=data)


@mini_all_goods_router.get("/dd_list", summary=f"查看点单{model_name}列表")
async def do_dd_list():
    category_q = Q(parent_id=1)
    category_total, category_data = await category_controller.list_category(page=1, page_size=1000, search=category_q)
    for category_item in category_data:
        category_id = category_item['id']
        q = Q(state=True)
        if category_id:
            q &= Q(category_id=category_id)
        total, data = await goods_controller.do_list(page=1, page_size=1000, search=q)
        for item in data:
            if item["sku"]:
                sku = item["sku"]
                kc, min_p, max_p = await goods_controller.sku_format(sku=sku)
                item["price"] = f"￥{min_p}~{max_p}"
                item["kc"] = kc
        category_item["foods"] = data
    return SuccessExtra(data=category_data, total=category_total, page=1, page_size=100)
